#include"../thread_pool.h"
#include"mysql.h"
extern ListNode * userList;
/*--------------------------delimiter----------------------------------------*/
int Add_user(user_info * user) {
    connect_mysql(user);
    //执行prepare操作，将stmt与模板化的sql语句绑定
    const char* sql = "INSERT INTO user_register (username, cryptpasswd, pwd) VALUES( ?, ?, ?)";
    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 3) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数

    char username[20] = {0};
    unsigned long name_len = 0;

    char cryptpasswd[512] = {0};
    unsigned long cryptpasswd_len = 0;

    char pwd[512] = {0};
    unsigned long pwd_len = 0;
    //绑定参数
    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[0].buffer = username;
    bind[0].is_null = 0;
    bind[0].length = &name_len;//指定字符串长度的缓冲区的首地址

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = cryptpasswd;
    bind[1].is_null = 0;
    bind[1].length = &cryptpasswd_len;

    bind[2].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[2].buffer = &pwd;
    bind[2].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[2].length = &pwd_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

     //第一次传递参数
    strcpy(username, user->name);
    name_len = strlen(username);
    strcpy(cryptpasswd, user->encrypt);
    cryptpasswd_len = strlen(cryptpasswd);
    strcpy(pwd,user->pwd);
    pwd_len = strlen(pwd);
    //第一次执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    } else {
        printf("QUERY OK, %ld row affected.\n",
               mysql_stmt_affected_rows(user->stmt));
        return 1;
    }
}

//char* str[]中存储的依次是username，cryptpasswd，pwd,
//第一个参数username是用来判断表中是否已经有注册信息
//若第一个参数不存在，说明不存在
/*---------------------------delimiter----------------------------------*/
void select_user(user_info* user, char* str[3] ) {
    connect_mysql(user);
    //初始化user
    const char* sql = "SELECT username,cryptpasswd,pwd FROM user_register WHERE username = ?\0";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 1) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    char user_name[20] = {0};
    unsigned long name_len = 0;

    //绑定参数
    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[0].buffer = &user_name;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &name_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    strcpy(user_name, user->name);
    printf("username is %s\n",user->name);
    name_len = strlen(user_name);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    char res_username[20] = {0};
    char res_cryptpassword[100] = {0};
    char res_pwd[100] = {0};
    MYSQL_BIND res_bind[3];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数

    res_bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[0].buffer = res_username;
    res_bind[0].buffer_length = sizeof(res_username);//确定值,空间要大

    res_bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[1].buffer = res_cryptpassword;
    res_bind[1].buffer_length = sizeof(res_cryptpassword);//确定值
                                                   //
    res_bind[2].buffer_type = MYSQL_TYPE_VAR_STRING;
    res_bind[2].buffer = res_pwd;
    res_bind[2].buffer_length = sizeof(res_pwd);//确定值
                                                //
    //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }

        str[0] = res_username;
        str[1] = res_cryptpassword;
        str[2] = res_pwd;
        //有可能释放完就为空
    }

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);
}
/*-------------------------delimiter-------------*/
void select_user_changeid(user_info* user) {
    connect_mysql(user);
    //初始化user
    const char* sql = "SELECT id FROM user_register WHERE username = ?\0";

    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 1) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit (1);
    }
    //设置参数
    char user_name[20] = {0};
    unsigned long name_len = 0;

    //绑定参数
    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[0].buffer = &user_name;
    bind[0].is_null = 0;//该参数不为空,设置为0；参数为空，设置为1
    bind[0].length = &name_len;//整型数据不需要指定长度缓冲区

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    strcpy(user_name, user->name);
    printf("username is %s\n",user->name);
    name_len = strlen(user_name);
    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if(ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //设置输出参数
    int res_id = 0;
    MYSQL_BIND res_bind[1];
    memset(res_bind, 0, sizeof(res_bind));
    //绑定输出参数

    res_bind[0].buffer_type = MYSQL_TYPE_LONG;
    res_bind[0].buffer = &res_id;
    res_bind[0].buffer_length = sizeof(res_id);//确定值,空间要大

    //执行绑定操作
    ret = mysql_stmt_bind_result(user->stmt, res_bind);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //再获取数据信息
    ret = mysql_stmt_store_result(user->stmt);
    if(ret) {
        printf("(%d, %s)\n", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //读取输出的数据
    while (1) {
        ret = mysql_stmt_fetch(user->stmt);
        if (ret == 1 || ret == MYSQL_NO_DATA) {
            break;
        }
        printf("res_id is %d\n",res_id);
        user->user_id = res_id;
        //有可能释放完就为空
    }

    //释放资源
    mysql_stmt_free_result(user->stmt);
    mysql_close(user->pconn);
}

